package com.woniuxy.jdkproxydemo;

import com.woniuxy.aspect.MyAspect;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Author 罗蔚
 * @Date 2022/3/28 14:17
 * @Description TODO
 */
public class MyServiceProxyClazz {
    public static Object createProxyObject(Object obj){
        Object proxyObject = Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(),
                (Object proxy, Method method, Object[] args) -> {
                    MyAspect myAspect = new MyAspect();
                    myAspect.log();
                    //就是我们反射出来的目标对象的方法
                    Object invoke = method.invoke(obj, args);
                    //后置方法
                    myAspect.commitTransation();
                    return invoke;
                });
        return proxyObject;
    }
}
